package com.masterclient.app.modules.bridge;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;

import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.Tools;
import com.eoner.managerlibrary.cookie.CookieManager;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.contract.MineContract;
import com.masterclient.app.businessmodule.homemodule.contract.MinePresenter;
import com.masterclient.app.modules.chezhubang.SelectMapDialog;
import com.masterclient.app.modules.income.model.MineDataBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import butterknife.BindView;
import wendu.dsbridge.DWebView;
import wendu.dsbridge.OnReturnValue;

public class BridgeFragment extends BaseFragment<MinePresenter> implements MineContract.View{

    @BindView(R.id.dwebview)
    DWebView dwebView;

    public static String URL = "url";
    private String url;
    private JsOfFragmentApi jsApi;
    public SelectMapDialog dialog;
    public boolean isBdMap = false,isGdMap = false,isTxMap = false;
    private boolean isScanResult = false;

    public static BridgeFragment newInstance(String url) {
        BridgeFragment bridgeFragment = new BridgeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        bridgeFragment.setArguments(bundle);
        return bridgeFragment;
    }

    public void refreshUrl(String url){
        this.url = url;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!TextUtils.isEmpty(SPUtils.getInstance().getString(Config.CUSTOMERID)) && !hidden) {
            if (dwebView != null) {
//                dwebView.loadUrl(url);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (dwebView != null) {
//            dwebView.loadUrl(url);
        }
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_bridge, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        WebSettings webSettings = dwebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setUserAgentString(webSettings.getUserAgentString() + " app:gm");
        init();
    }

    private void init(){
        CookieManager.setAppInfoCookies(getActivity());
        dialog = new SelectMapDialog(getActivity());
        setDialogData();
        url = getArguments().getString(URL);

        jsApi = new JsOfFragmentApi(this);
        dwebView.addJavascriptObject(jsApi, null);
        dwebView.loadUrl(url);
    }

    public void reLoad(){
        if (dwebView != null){
            dwebView.loadUrl(url);
        }
    }

    public void scanResult(String code){
        isScanResult = true;
       jsApi.scanResult(code);
    }

    private void setDialogData(){
        isBdMap = Tools.isAppAvilible(getActivity(),"com.baidu.BaiduMap");
        isGdMap = Tools.isAppAvilible(getActivity(),"com.autonavi.minimap");
        isTxMap = Tools.isAppAvilible(getActivity(),"com.tencent.map");
    }

    public void setDialog(Map<String , String> map){
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isBdMap||isGdMap||isTxMap){
                    dialog.show();
                    dialog.setVisiable(isGdMap,isBdMap,isTxMap);
                    dialog.setData(map.get("lng"),map.get("lat"),
                            map.get("lng"),map.get("lat"),"");
                }else {
                    showToast("请下载高德百度腾讯地图软件进行导航");
                }
            }
        });
    }

    @Override
    public void getCustomerInfoSuccess(MineDataBean mineDataBean) {

    }

    @Override
    public void getCustomerInfoFailed(String msg) {

    }

    @Override
    public void logout() {

    }

    @Override
    public void logoutFailed(String msg) {
    }

    @Override
    public void cancelSuccess() {

    }

    @Override
    public void cancelFailed(String msg) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
